Project Breakdown
This comprehensive guide provides an in-depth look at the AssemblyScript Stylus SDK's architecture, components, and development workflow. After completing the Quick Start guide, this breakdown will help you understand the full capabilities and design philosophy of the SDK.
Platform Components
Our monorepo contains three main components designed to provide a complete development experience:
🛠️ SDK Core
The main SDK package that provides:
- AssemblyScript bindings for Stylus host functions
- Type-safe storage and memory management
- Event emission and error handling
- CLI tools for project scaffolding, compilation, and deployment
- Built-in support for common standards (ERC20, ERC721)
📚 Documentation Website
Comprehensive documentation covering:
- Getting started guides and tutorials
- API reference and examples
- Best practices and patterns
- Migration guides and troubleshooting
Key Features
- TypeScript/AssemblyScript Syntax: Write contracts using familiar TypeScript decorators and syntax
- Type Safety: Full compile-time type checking and validation
- Performance: Compile to WebAssembly for near-native execution speeds on Stylus
- Developer Experience: Comprehensive CLI tooling and familiar development workflow
- Rich Type System: Support for U256, I256, Address, String, Boolean, Mapping, and Struct types
- Event System: Emit events with proper ABI encoding
- Error Handling: Custom error types with revert functionality
- Standard Library: Built-in implementations of common contract patterns
Developer Workflow
The SDK provides a streamlined development workflow from scaffolding to deployment:
Core Concepts
Decorators
Decorators define the behavior and visibility of your contract components:
- @Contract: Marks a class as a smart contract entry point
- @External: Makes methods callable from external transactions
- @View: Marks read-only methods that don't modify state
- @Event: Defines event structures for logging
- @Error: Creates custom error types for revert functionality
Data Types
The SDK provides blockchain-optimized types for efficient development:
- U256/I256: 256-bit unsigned/signed integers with arithmetic operations
- Address: Ethereum addresses with built-in validation
- String: Dynamic strings with storage optimization
- Boolean: Boolean values with proper 32-byte storage alignment
- Mapping: Persistent key-value storage mappings
- Struct: Custom data structures with automatic serialization
Project Structure
When you generate a new project, you'll get this structure:
my-contract/
├── contract.ts # Your main contract file
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
└── artifacts/ # Generated build artifacts
├── build/ # Compiled WASM files
└── abi/ # Contract ABI files
File Breakdown
- contract.ts: Your main contract implementation with TypeScript decorators
- package.json: Contains project dependencies and helpful npm scripts for building and deploying
- tsconfig.json: TypeScript configuration optimized for AssemblyScript compilation
- artifacts/: Generated during compilation, contains WASM bytecode and ABI definitions
Architecture Deep Dive
Compilation Pipeline
- TypeScript Source: Your contract written with familiar TypeScript syntax
- AssemblyScript Transpilation: TypeScript decorators are processed and converted to AssemblyScript
- WebAssembly Compilation: AssemblyScript is compiled to optimized WASM bytecode
- Stylus Validation: The WASM is validated for compatibility with Arbitrum Stylus
- Deployment: Contract is deployed to Arbitrum with proper initialization
Memory Management
The SDK provides efficient memory management through:
- Stack-based Storage: For temporary variables and function parameters
- Persistent Storage: For contract state using optimized storage slots
- Memory Pools: For dynamic allocations with automatic cleanup
Gas Optimization
- Efficient Encoding: Automatic optimization of data serialization
- Storage Patterns: Optimized storage layouts to minimize gas costs
- Function Inlining: Compile-time optimizations for frequently used operations
Development Best Practices
Contract Design
- State Management: Use appropriate data types for your use case
- Function Visibility: Properly mark functions as @External or @View
- Error Handling: Implement comprehensive error checking with custom errors
- Event Emission: Log important state changes for off-chain monitoring
Testing Strategy
- Unit Tests: Test individual functions and components
- Integration Tests: Test contract interactions and workflows
- Gas Analysis: Monitor gas usage and optimize performance
- Security Audits: Review contracts for potential vulnerabilities
Comprehensive Learning Path
Learn the Fundamentals
- Decorators - Define contract structure and behavior
- Data Types - Master blockchain-optimized types
- Data Structures - Organize contract state efficiently
Build Real Contracts
- ERC20 Example - Token contract implementation
- ERC721 Example - NFT contract patterns
Advanced Topics
- Error Handling - Custom error types and patterns
- Event System - Efficient logging and monitoring
- Visibility Modifiers - Access control patterns
Resources
Next Steps
Ready to build the future of smart contracts? Here are your next steps:
- Start with the Basics: Master the decorator system and understand how to structure contracts
- Learn the Type System: Dive deep into U256 and other blockchain-optimized types
- Build Your First Token: Follow the ERC20 tutorial to create a complete token contract
- Explore Advanced Features: Learn about events, errors, and data structures
- Deploy to Mainnet: Use the CLI tools to deploy your contracts to Arbitrum
The AssemblyScript Stylus SDK opens up new possibilities for high-performance smart contract development. With familiar TypeScript syntax and WebAssembly performance, you can build the next generation of decentralized applications!